package com.yiwei.zxp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.adapter.SearchHistoryAdapter;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.util.Dialog;
import com.yiwei.zxp.util.PreferenceUtils;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.UI;
import com.yiwei.zxp.util.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class SearchActivity extends BaseActivity {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.edit_search)
    EditText editSearch;
    @BindView(R.id.btn_search)
    TextView btnSearch;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.btn_delete)
    LinearLayout btnDelete;
    private SearchHistoryAdapter adapter;
    private Unbinder unbiner;
    private List<String> mHistoryKeywords;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_search);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        adapter = new SearchHistoryAdapter(this);
        list.setAdapter(adapter);
        mHistoryKeywords = new ArrayList<String>();

        String history = PreferenceUtils.getString(this, "search_data", "");
        if (!TextUtils.isEmpty(history)) {
            List<String> list = new ArrayList<String>();
            for (Object o : history.split(",")) {
                list.add((String) o);
            }
            mHistoryKeywords = list;
            adapter.update(mHistoryKeywords);
        }
    }

    public void save() {
        String text = UI.toString(editSearch);
        String oldText = PreferenceUtils.getString(this, "search_data", "");
        if (!TextUtils.isEmpty(text) && !oldText.contains(text)) {
            PreferenceUtils.putString(this, "search_data", text + "," + oldText);
            mHistoryKeywords.add(0, text);
        }
        adapter.update(mHistoryKeywords);
    }

    public void cleanHistory() {
        PreferenceUtils.remove(this, "search_data");
        mHistoryKeywords.clear();
        adapter.update(mHistoryKeywords);
        ToastUtil.makeText(this, "删除成功");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_clear, R.id.btn_back, R.id.btn_search, R.id.btn_delete})
    public void onViewClicked(View view) {
        if (UI.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_clear:
                editSearch.setText("");
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_search:
                if (TextUtils.isEmpty(UI.toString(editSearch))) {
                    ToastUtil.makeText(this, "搜索内容不能为空");
                    return;
                }
                save();
                Intent intent = new Intent(this, SearchResultActivity.class);
                intent.putExtra("keyword", UI.toString(editSearch));
                startActivity(intent);
                break;
            case R.id.btn_delete:
                Dialog.showDefaultDialog(this, "", "确定要删除搜索记录吗？", "取消", "确定", false, new Dialog.DialogDefaultClickListener() {
                    @Override
                    public void confirm() {
                        cleanHistory();
                    }

                    @Override
                    public void cancel() {

                    }
                });

                break;
        }
    }
}
